#include "ASprite.h"

	ASprite::ASprite(float x, float y, int w, int h, SDL_Surface *spritesheet, int reels, int frames): Sprite(x, y, w, h, spritesheet)
	{
		Image = new SDL_Rect();

		currentframe = 0;
		maxframes = frames;
		currentreel = 0;
		maxreels = reels;
		oscillate = false;
		rev = false;
		framerate = 0;
		oldtime = 0;
		animated = false;
		(*Image).x = 0;
		(*Image).y = 0;
		(*Image).w = w;
		(*Image).h = h;
		time = 0;
		rlower = 0;
		rupper = frames;
	}
	
	ASprite::ASprite(): Sprite(0, 0, 0, 0, NULL)
	{
		Image = new SDL_Rect();

		currentframe = 0;
		maxframes = 0;
		currentreel = 0;
		maxreels = 0;
		oscillate = false;
		rev = false;
		framerate = 0;
		oldtime = 0;
		animated = false;
		(*Image).x = 0;
		(*Image).y = 0;
		(*Image).w = 0;
		(*Image).h = 0;
		time = 0;
		rlower = rupper = 0;
	}

	void ASprite::update(Uint32 ticks)
	{
		if(animated)
		{
			time += ticks;

			if(time >= framerate)
			{
				time = 0;

				nextframe();
			}
		}
	}

	void ASprite::setFramerate(float rate)
	{
		framerate = 1000.f/rate;
	}

	void ASprite::setCurrentframe(int frame)
	{
		currentframe = frame;
	}

	void ASprite::setCurrentreel(int reel)
	{
		currentreel = reel;
	}
	
	void ASprite::setOscillation(bool osc)
	{
		oscillate = osc;
		if(osc == false)
			rev = false;
		else if(osc == true)
		{
			if(currentframe == maxframes - 1)
				rev = true;
		}
	}

	void ASprite::animate(bool anim)
	{
		animated = anim;
	}

	int ASprite::getCurrentframe()
	{
		return currentframe;
	}

	int ASprite::getCurrentreel()
	{
		return currentreel;
	}

	void ASprite::nextframe()
	{
		//Default
		//No oscillation
		if(oscillate == false)
		{
			currentframe++;
			if(currentframe == maxframes)
				currentframe = 0;
		}
		//Oscillation enabled
		else if(oscillate == true)
		{
			//If not reversed
			if(!rev)
			{
				currentframe++;
				if(currentframe >= (maxframes - 1))
					rev = true;
			}
			//If reversed
			else if(rev)
			{
				currentframe--;
				if(currentframe <= 0)
					rev = false;
			}
		}
	}

	SDL_Rect* ASprite::getClip()
	{
		(*Image).x = w * currentframe;
		(*Image).y = h * currentreel;
		return Image;
	}
